package com.cenxi.service;

import com.cenxi.util.contents.Content;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @Author cenxi
 * @Date 2023/9/7 10:40
 * @Version 1.0
 */
@Component
@Slf4j
public class AsyncService {
    @Autowired
    private RedissonClient redissonClient;

    @Async("taskExecutor")
    public Object async() {
        RLock lock = redissonClient.getLock(Content.LOCK);
        lock.lock();
        try {
            System.out.println("加锁成功，执行业务代码..."+Thread.currentThread().getId());
            Thread.sleep(50000);
            return "加锁成功..."+Thread.currentThread().getId();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("释放锁..."+Thread.currentThread().getId());
            lock.unlock();
        }
        return "hello..."+Thread.currentThread().getId();
    }
}
